/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.ProductDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.PageHelper;
import com.minierp.model.Product;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class ProductDaoImpl extends BaseDaoImpl implements ProductDao {
	public List<?> getList(long productTypeId, String searchKey,
			boolean justEnabled, boolean showStock, String stocType, long stoId) {
		String fieldStr = "s.id,s.product_code,s.product_name,s.unit,s.attach_file,s.product_type_id,s.mark,t.type_name";
		String tableStr = "product s left join product_type t on s.product_type_id=t.id";
		if (showStock) {
			fieldStr = fieldStr + ",st.amount";
			tableStr = tableStr
					+ " left join stock st on s.id=st.product_id and st.storehouse_id="
					+ stoId;
		} else {
			fieldStr = fieldStr + ",0 as amount";
		}

		String sql = "select " + fieldStr + " from " + tableStr + " where 1=1";
		if (productTypeId != 0L) {
			sql = sql + " and (t.id=" + productTypeId
					+ " or (t.parent_id_str like \'%;" + productTypeId
					+ ";%\'))";
		}

		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			sql = sql
					+ " and ((s.product_name like :productName) or (s.product_code like :productCode) or (s.pn_spell like :pnSpell))";
		}

		if (justEnabled) {
			sql = sql + " and s.enabled=\'Y\'";
		}

		if (showStock && stocType != null && stocType.equals("UPPER_ZERO")) {
			sql = sql + " and st.amount>0";
		}

		sql = sql + " order by t.name_spell,s.product_code";
		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				String pnSpell = searchKey.toUpperCase();
				q.setString("productName", "%" + searchKey + "%");
				q.setString("productCode", "%" + searchKey + "%");
				q.setString("pnSpell", "%" + pnSpell + "%");
			}

			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public Product get(String productCode, boolean justEnabled) {
		String sql = "from com.minierp.model.Product as obj where obj.productCode = :productCode";
		if (justEnabled) {
			sql = sql + " and obj.enabled=\'Y\'";
		}

		sql = sql + " order by obj.id";
		String[] paramNames = new String[]{"productCode"};
		Object[] values = new Object[]{productCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (Product) list.get(0);
	}

	public Product getByName(String productName, boolean justEnabled) {
		String sql = "from com.minierp.model.Product as obj where obj.productName = :productName";
		if (justEnabled) {
			sql = sql + " and obj.enabled=\'Y\'";
		}

		sql = sql + " order by obj.id";
		String[] paramNames = new String[]{"productName"};
		Object[] values = new Object[]{productName};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (Product) list.get(0);
	}

	public List<?> getList(long[] ids) {
		if (ids.length > 0) {
			String idStr = PageHelper.createIdArrStr(ids, "p.id", false);
			return (List) (idStr.equals("") ? new ArrayList() : this
					.getHibernateTemplate().find(
							"from com.minierp.model.Product p where " + idStr));
		} else {
			return new ArrayList();
		}
	}

	public List<?> getList(long productTypeId, String productCode,
			String productName) {
		String sql = "select s from com.minierp.model.Product s, com.minierp.model.ProductType t where s.productTypeId=t.id";
		if (productTypeId != 0L) {
			sql = sql + " and (t.id=" + productTypeId
					+ " or (t.parentIdStr like \'%;" + productTypeId + ";%\'))";
		}

		if (productCode != null) {
			productCode = productCode.trim();
		}

		if (productCode != null && !productCode.equals("")) {
			sql = sql + " and (s.productCode like :productCode)";
		}

		if (productName != null) {
			productName = productName.trim();
		}

		if (productName != null && !productName.equals("")) {
			sql = sql + " and (s.productName like :productName)";
		}

		Session sess = null;

		List arg8;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (productCode != null && !productCode.equals("")) {
				q.setString("productCode", "%" + productCode + "%");
			}

			if (productName != null && !productName.equals("")) {
				q.setString("productName", "%" + productName + "%");
			}

			arg8 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public int getCount(long typeId) {
		String sql = "select count(s.id) from com.minierp.model.Product s ,com.minierp.model.ProductType d where s.productTypeId=d.id and s.enabled=\'Y\' and (d.id="
				+ typeId + " or (d.parentIdStr like \'%;" + typeId + ";%\'))";
		List list = this.getHibernateTemplate().find(sql);
		return list.isEmpty() ? 0 : Integer.parseInt(list.get(0).toString());
	}

	public int update(long[] ids, final long typeId) {
		if (ids != null && ids.length != 0) {
			final String tmpStr = "";

			for (int idStr = 0; idStr < ids.length; ++idStr) {
				if (!tmpStr.equals("")) {
					tmpStr = tmpStr + ",";
				}

				tmpStr = tmpStr + ids[idStr];
			}

			Object obj = this.getHibernateTemplate().execute(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							String hql = "update com.minierp.model.Product set productTypeId="
									+ typeId + " where id in (" + tmpStr + ")";
							return new Integer(session.createQuery(hql)
									.executeUpdate());
						}
					});
			return ((Integer) obj).intValue();
		} else {
			return 0;
		}
	}

	public List<?> getList() {
		String sql = "select p,pt from com.minierp.model.Product p,com.minierp.model.productType pt where p.productTypeId=pt.id and p.enabled=\'Y\'";
		List list = this.getHibernateTemplate().find(sql);
		return list;
	}

	public List getExisting(long storehouseId) {
		String sql = "select p, s from com.minierp.model.Product p,  com.minierp.model.Stock s  where s.productId=p.id  and s.storehouseId="
				+ storehouseId + " and s.amount>0 " + " and p.enabled=\'Y\'";
		return this.getHibernateTemplate().find(sql);
	}

	public List getListByStorehouseId(long storehouseId) {
		String sql = "select p, s from com.minierp.model.Product p,  com.minierp.model.Stock s  where s.productId=p.id  and s.storehouseId="
				+ storehouseId + " and p.enabled=\'Y\' ";
		return this.getHibernateTemplate().find(sql);
	}
}